package com.hz.filemanager.ui.file.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.hz.filemanager.R;
import com.hz.filemanager.expendable_recyclerview.BaseExpandableRecyclerViewAdapter;
import com.hz.filemanager.model.file.FileManager;
import com.hz.filemanager.model.file.Video;
import com.hz.filemanager.model.file.VideoFolderBean;
import com.hz.filemanager.utils.Utils;
import com.kathline.library.util.ZFileOtherUtil;
import com.kathline.library.util.ZFileUtil;

import java.util.ArrayList;
import java.util.List;

public class VideoAdapter extends BaseExpandableRecyclerViewAdapter<VideoFolderBean, Video, VideoAdapter.GroupVH, VideoAdapter.ChildVH>{

    private List<VideoFolderBean> datas = new ArrayList<>();
    private Context context;
    private OnItemClickListener onItemClickListener;
    private OnLongClickListener onLongClickListener;
    private boolean isSelectModel = false;

    private List<Video> selectVideoList = new ArrayList<>();

    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setSelectModel(boolean selectModel) {
        isSelectModel = selectModel;
    }

    public void checkAllOf(boolean isCheck){
        selectVideoList.clear();
        if (isCheck){
            for (VideoFolderBean data : datas) {
                selectVideoList.addAll(new ArrayList<>(data.getVideos()));
            }
        }else {
            //ignore
        }
        notifyDataSetChanged();
    }

    public boolean isSelectModel() {
        return isSelectModel;
    }

    public List<VideoFolderBean> getDatas() {
        return datas;
    }

    public void setDatas(List<VideoFolderBean> datas) {
        if(datas == null || datas.isEmpty()){
            this.datas.clear();
        }else{
            this.datas.clear();
            this.datas.addAll(datas);
        }
        notifyDataSetChanged();
    }

    public void setSelectVideoList(List<Video> selectVideoList) {
        this.selectVideoList = selectVideoList;
    }

    public List<Video> getSelectVideoList() {
        return selectVideoList;
    }

    public VideoAdapter(Context context){
        super();
        this.context = context;
    }
    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public VideoFolderBean getGroupItem(int groupIndex) {
        return datas.get(groupIndex);
    }

    @Override
    public GroupVH onCreateGroupViewHolder(ViewGroup parent, int groupViewType) {
        return new GroupVH(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listitem_group, parent, false));
    }

    @Override
    public void onBindGroupViewHolder(GroupVH holder, VideoFolderBean groupBean, boolean isExpand,int position) {
        holder.nameTv.setText(groupBean.getName());
        holder.groupSizeTv.setText("("+groupBean.getChildCount()+")");
        if (groupBean.isExpandable()) {
            holder.foldIv.setVisibility(View.VISIBLE);
            holder.foldIv.setImageResource(isExpand ? R.mipmap.icon_expand : R.mipmap.icon_no_expand);
        } else {
            holder.foldIv.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public ChildVH onCreateChildViewHolder(ViewGroup parent, int childViewType) {
        return new ChildVH(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_child_video, parent, false));
    }

    @Override
    public void onBindChildViewHolder(ChildVH holder, VideoFolderBean groupBean, Video video,int position) {

        Glide.with(context)
                .load(FileManager.getInstance(Utils.getContext()).getVideoThumbnail(video.getId()))
                .transform(new CenterCrop(),new RoundedCorners(20))
                .placeholder(R.color.cbcbcb)
                .error(R.color.cbcbcb)
                .into(holder.videoIv);
        holder.name.setText(video.getName());
        holder.date.setText(ZFileOtherUtil.getFormatFileDate(video.getDate()*1000));
        holder.size.setText(ZFileUtil.getFileSize(video.getSize()));

        if(isSelectModel){
            holder.cb.setVisibility(View.VISIBLE);
            holder.cb.setChecked(selectVideoList.contains(video));

        }else{
            holder.cb.setVisibility(View.INVISIBLE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isSelectModel){
                    if(selectVideoList.contains(video)){
                        selectVideoList.remove(video);
                        holder.cb.setChecked(false);
                    }else{
                        selectVideoList.add(video);
                        holder.cb.setChecked(true);
                    }
                }

                if(onItemClickListener != null){
                    onItemClickListener.onClick(v, groupBean, video);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                if(onLongClickListener != null && !isSelectModel){
                    onLongClickListener.onLongClick( groupBean, video);
                }
                return true;
            }
        });
    }

    public void clear() {
        this.datas.clear();
        notifyDataSetChanged();
    }

    static class GroupVH extends BaseExpandableRecyclerViewAdapter.BaseGroupViewHolder {
        TextView groupSizeTv;
        ImageView foldIv;
        TextView nameTv;

        GroupVH(View itemView) {
            super(itemView);
            foldIv = (ImageView) itemView.findViewById(R.id.group_item_indicator);
            nameTv = (TextView) itemView.findViewById(R.id.group_item_name);
            groupSizeTv = itemView.findViewById(R.id.group_size);
        }

        @Override
        protected void onExpandStatusChanged(RecyclerView.Adapter relatedAdapter, boolean isExpanding) {
            foldIv.setImageResource(isExpanding ? R.mipmap.icon_expand : R.mipmap.icon_no_expand);
        }
    }

    static class ChildVH extends RecyclerView.ViewHolder {
        private final TextView name;
        private final TextView size;
        private final TextView date;
        private final CheckBox cb;
        ImageView videoIv;

        ChildVH(View itemView) {
            super(itemView);
            videoIv = (ImageView) itemView.findViewById(R.id.video_iv);
            name = itemView.findViewById(R.id.video_name);
            size = itemView.findViewById(R.id.video_size);
            date =  itemView.findViewById(R.id.video_date);
            cb = itemView.findViewById(R.id.cb);
        }
    }

    public static interface OnItemClickListener{
        void onClick(View view, VideoFolderBean groupBean, Video video);
    }
    public static interface OnLongClickListener{
        void onLongClick( VideoFolderBean groupBean, Video video);
    }

}
